package com.springboot.frame;

import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import cn.easyes.starter.register.EsMapperScan;
import com.springboot.frame.mapper.DocumentMapper;
import com.springboot.frame.model.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@RestController
@EsMapperScan("com.springboot.frame.mapper")
@SpringBootApplication
public class SpringbootElasticsearchApplication {

    @Autowired
    private DocumentMapper documentMapper;

    public static void main(String[] args) {
        SpringApplication.run(SpringbootElasticsearchApplication.class, args);
    }

    @GetMapping("/delete")
    public String delete() {
        documentMapper.delete(new LambdaEsQueryWrapper<Document>().isNotNull(Document::getContent));
        return "succ";
    }

    @GetMapping("/insert")
    public String insert() {
        readFile();
        return "succ";
    }

    @GetMapping("/search")
    public List<Document> search(@RequestParam("content") String content) {
        // 查询出所有标题为老汉的文档列表
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(Document::getContent, content);
        return documentMapper.selectList(wrapper);
    }


    public void readFile() {
        try {
            FileInputStream fis = new FileInputStream("C:\\Users\\lihen\\Documents\\WXWork\\1688850865404151\\Cache\\File\\2022-10\\2021126-bill12396-87w.txt");
            // 防止路径乱码   如果utf-8 乱码  改GBK     eclipse里创建的txt  用UTF-8，在电脑上自己创建的txt  用GBK
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line = "";
            Document document;
            List<Document> documents = new ArrayList<>();
            int i = 0;
            while ((line = br.readLine()) != null) {
                // 如果 t x t文件里的路径 不包含---字符串       这里是对里面的内容进行一个筛选
                // 初始化-> 新增数据
                document = new Document();
                document.setContent(line);
                documents.add(document);
                i++;
                if (i == 100000) {
                    documentMapper.insertBatch(documents);
                    documents.clear();
                    i = 0;
                }
            }
            if(documents.size()>0) {
                documentMapper.insertBatch(documents);
            }
            br.close();
            isr.close();
            fis.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
